﻿// Copyright (c) Inossem. All rights reserved.
// Licensed under the LGPL-3.0 license. See LICENSE file in the project root for full license information.
using DWF.Studio.Custom;
using DWF.Studio.Project;
using DWF.Studio.Script;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WpfMessageBoxLibrary;

namespace DWF.Studio.Views
{
    /// <summary>
    /// WindowProjectInfo.xaml 的交互逻辑
    /// </summary>
    public partial class WindowProjectProperties
    {
        private ProjectManager _projectManager = null;

        public WindowProjectProperties()
        {
            InitializeComponent();
        }

        private void CustomWindow_Loaded(object sender, RoutedEventArgs e)
        {
            _projectManager = ProjectManager.Instance;

            var projectInfo = _projectManager.CurrentProject;

            if (projectInfo != null)
            {
                tbName.Text = projectInfo.DisplayName;
                tbId.Text = projectInfo.Id;
                tbDescription.Text = projectInfo.Description;

                if (projectInfo.TargetPlatform == StaticResource.TargetPlatform_x86)
                {
                    rbtnX86.IsChecked = true;
                }
                else if (projectInfo.TargetPlatform == StaticResource.TargetPlatform_x64)
                {
                    rbtnX64.IsChecked = true;
                }

                if (projectInfo.WFLanguage == StaticResource.WorkflowLanguage_vb)
                {
                    rbtnVb.IsChecked = true;
                }
                else if (projectInfo.WFLanguage == StaticResource.WorkflowLanguage_cs)
                {
                    rbtnCs.IsChecked = true;
                }
            }
        }

        private void BtnConfirm_Click(object sender, RoutedEventArgs e)
        {
            _projectManager.ModifyProjectId(tbId.Text);
            Color color = (Color)ColorConverter.ConvertFromString("#EA5050");
            SolidColorBrush brush = new SolidColorBrush(color);
            bool isRegex = Regex.IsMatch(tbName.Text, "[*\\\":?><|/]");
            if (String.IsNullOrWhiteSpace(tbName.Text) || isRegex || this.tbName.Text.Contains("\\"))
            {
                var msgProperties = new WpfMessageBoxProperties()
                {
                    Button = MessageBoxButton.OK,
                    ButtonOkText = LocalizationProvider.GetLocalizedString("MessageBox_Button_Ok"),
                    Image = MessageBoxImage.Warning,
                    Text = string.Format(LocalizationProvider.GetLocalizedString("WindowProjectProperties_Msg_ProjectName_Error")),
                    Title = LocalizationProvider.GetLocalizedString("WindowCreateProject_MsgBox_Caption_Create_NameValidate"),
                };
                WpfMessageBox.Show(this, ref msgProperties);

                Keyboard.Focus(tbName);
                tbName.BorderBrush = brush;
                return;
            }
            //var isReload = false;
            if (_projectManager.CurrentProject != null)
            {
                if (_projectManager.CurrentProject.DisplayName != tbName.Text)
                {
                    _projectManager.ModifyProjectName(tbName.Text);
                    _projectManager.ModifyProjectId(Guid.NewGuid().ToString("N"));
                    //isReload = true;
                    GlobalActionHandler.RefreshTitleName?.Invoke(new TagEventArgs<string>(tbName.Text));
                }
            }
            _projectManager.ModifyProjectDescription(tbDescription.Text);


            if (rbtnX86.IsChecked == true)
            {
                _projectManager.ModifyTargetPlatform(StaticResource.TargetPlatform_x86);
            }
            else if (rbtnX64.IsChecked == true)
            {
                _projectManager.ModifyTargetPlatform(StaticResource.TargetPlatform_x64);
            }

            if (rbtnVb.IsChecked == true)
            {
                _projectManager.ModifyWFLanguage(StaticResource.WorkflowLanguage_vb);
            }
            else if (rbtnCs.IsChecked == true)
            {
                _projectManager.ModifyWFLanguage(StaticResource.WorkflowLanguage_cs);
            }

            this.Close();
        }

        private void BtnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
        /// <summary>
        /// 改变项目id
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnChangeId_Click(object sender, RoutedEventArgs e)
        {
            tbId.Text = Guid.NewGuid().ToString("N");
            btnChangeId.IsEnabled = false;
        }

        private void tbName_TextChanged(object sender, TextChangedEventArgs e)
        {
            Color color = (Color)ColorConverter.ConvertFromString("#BCBCBC");
            SolidColorBrush brush = new SolidColorBrush(color);
            tbName.BorderBrush = brush;
            return;
        }
    }
}
